import { response } from "express"
import { request } from "express"
import CODE from "../../config/data.config.js"
import MenuService from "../../service/menuService/menuService.js"
import { ForestTree } from "../../util/DataStrcutures.js"
import * as service from "../../service/service.js"
class MenuController {
    /**
     * @param {request} req
     * @param {response} res
     * @param {Function} next
     * get
     */
    static async GetMenuList(req, res, next) {
        try {
            let {isAdmin} = req.query
            let result = await MenuService.GetMenuList(isAdmin)
            // 菜单栏需要转换为森林结构
            res.send({
                msg: '查询菜单成功！',
                code: CODE.CODE_OK,
                data: ForestTree.ToForest(result)
            })
        } catch (error) {
            next({ error })
        }
    }

    /**
     * @param {request} req
     * @param {response} res
     * @param {Function} next
     */
    static async InsertMenu(req, res, next) {
        try {
            let param = req.body
            let status = await MenuService.InsertMenu(param)
            if (status) {
                res.send({
                    msg: '插入菜单成功!',
                    code: CODE.CODE_OK
                })
            }
            else next({
                msg: '插入菜单失败!',
                code: CODE.CODE_FAIL,
                error: {}
            })
        } catch (error) {
            next(error)
        }
    }

    /**
     * @param {request} req 
     * @param {response} res 
     * @param {function} next 
     */
    static async GetMenuRootList(req, res, next) {
        try {
            let result = await service.MenuService.GetMenuRootList()
            return res.send({
                msg: '获取菜单成功',
                code: CODE.CODE_OK,
                data: ForestTree.ToForest(result)
            }).end()
        } catch (error) {
            next({ error })
        }
    }

    /**
     * @param {request} req 
     * @param {response} res 
     * @param {function} next 
     */
    static async DeleteMenu(req, res, next) {
        try {
            let { id } = req.body
            let state = await service.MenuService.DeleteMenu(id)
            state ? res.send({
                msg: '删除成功',
                code: CODE.CODE_OK,
            }) : res.send({
                msg: '删除失败',
                code: CODE.CODE_FAIL
            })
        } catch (error) {
            next({ error })
        }
    }

    /**
     * 修改菜单 put请求
     * @param {request} req 
     * @param {response} res 
     * @param {function} next 
     */
    static async UpdateMenu(req, res, next) {
        try {
            let param = req.body
            let { } = await service.MenuService.UpdateMenu(param)
            
        } catch (error) {
            
        }
    }
}


export default MenuController;